home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Utilities / Calc / config.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-24  |  5.7 KB  |  296 lines  |  [TEXT/????]

  1. /*
  2.  * Copyright (c) 1992 David I. Bell
  3.  * Permission is granted to use, distribute, or modify this source,
  4.  * provided that this copyright notice remains intact.
  5.  *
  6.  * Configuration routines.
  7.  */
  8.  
  9. #include "calc.h"
  10.  
  11.  
  12. /*
  13.  * Configuration parameter name and type.
  14.  */
  15. typedef struct {
  16.     char *name;    /* name of configuration string */
  17.     int type;    /* type for configuration */
  18. } CONFIG;
  19.  
  20.  
  21. /*
  22.  * Table of configuration types that can be set or read.
  23.  */
  24. static CONFIG configs[] = {
  25.     "trace",    CONFIG_TRACE,
  26.     "display",    CONFIG_DISPLAY,
  27.     "epsilon",    CONFIG_EPSILON,
  28.     "mode",        CONFIG_MODE,
  29.     "maxprint",    CONFIG_MAXPRINT,
  30.     "mul2",        CONFIG_MUL2,
  31.     "sq2",        CONFIG_SQ2,
  32.     "pow2",        CONFIG_POW2,
  33.     "redc2",    CONFIG_REDC2,
  34.     NULL,        0
  35. };
  36.  
  37.  
  38. /*
  39.  * Possible output modes.
  40.  */
  41. static CONFIG modes[] = {
  42.     "frac",        MODE_FRAC,
  43.     "decimal",    MODE_FRAC,
  44.     "dec",        MODE_FRAC,
  45.     "int",        MODE_INT,
  46.     "real",        MODE_REAL,
  47.     "exp",        MODE_EXP,
  48.     "hexadecimal",    MODE_HEX,
  49.     "hex",        MODE_HEX,
  50.     "octal",    MODE_OCTAL,
  51.     "oct",        MODE_OCTAL,
  52.     "binary",    MODE_BINARY,
  53.     "bin",        MODE_BINARY,
  54.     NULL,        0
  55. };
  56.  
  57.  
  58. /*
  59.  * Given a string value which represents a configuration name, return
  60.  * the configuration type for that string.  Returns negative type if
  61.  * the string is unknown.
  62.  */
  63. configtype(name)
  64.     char *name;        /* configuration name */
  65. {
  66.     CONFIG *cp;        /* current config pointer */
  67.  
  68.     for (cp = configs; cp->name; cp++) {
  69.         if (strcmp(cp->name, name) == 0)
  70.             return cp->type;
  71.     }
  72.     return -1;
  73. }
  74.  
  75.  
  76. /*
  77.  * Given the name of a mode, convert it to the internal format.
  78.  * Returns -1 if the string is unknown.
  79.  */
  80. static
  81. modetype(name)
  82.     char *name;        /* mode name */
  83. {
  84.     CONFIG *cp;        /* current config pointer */
  85.  
  86.     for (cp = modes; cp->name; cp++) {
  87.         if (strcmp(cp->name, name) == 0)
  88.             return cp->type;
  89.     }
  90.     return -1;
  91. }
  92.  
  93.  
  94. /*
  95.  * Given the mode type, convert it to a string representing that mode.
  96.  * Where there are multiple strings representing the same mode, the first
  97.  * one in the table is used.  Returns NULL if the node type is unknown.
  98.  * The returned string cannot be modified.
  99.  */
  100. static char *
  101. modename(type)
  102. {
  103.     CONFIG *cp;        /* current config pointer */
  104.  
  105.     for (cp = modes; cp->name; cp++) {
  106.         if (type == cp->type)
  107.             return cp->name;
  108.     }
  109.     return NULL;
  110. }
  111.  
  112.  
  113. /*
  114.  * Set the specified configuration type to the specified value.
  115.  * An error is generated if the type number or value is illegal.
  116.  */
  117. void
  118. setconfig(type, vp)
  119.     VALUE *vp;
  120. {
  121.     NUMBER *q;
  122.     long temp;
  123.  
  124.     switch (type) {
  125.         case CONFIG_TRACE:
  126.             if (vp->v_type != V_NUM)
  127.                 error("Non-numeric for trace");
  128.             q = vp->v_num;
  129.             temp = qtoi(q);
  130.             if (qisfrac(q) || !istiny(q->num) ||
  131.                 ((unsigned long) temp > TRACE_MAX))
  132.                     error("Bad trace value");
  133.             traceflags = (FLAG)temp;
  134.             break;
  135.  
  136.         case CONFIG_DISPLAY:
  137.             if (vp->v_type != V_NUM)
  138.                 error("Non-numeric for display");
  139.             q = vp->v_num;
  140.             temp = qtoi(q);
  141.             if (qisfrac(q) || qisneg(q) || !istiny(q->num))
  142.                 temp = -1;
  143.             setdigits(temp);
  144.             break;
  145.  
  146.         case CONFIG_MODE:
  147.             if (vp->v_type != V_STR)
  148.                 error("Non-string for mode");
  149.             temp = modetype(vp->v_str);
  150.             if (temp < 0)
  151.                 error("Unknown mode \"%s\"", vp->v_str);
  152.             setmode((int) temp);
  153.             break;
  154.  
  155.         case CONFIG_EPSILON:
  156.             if (vp->v_type != V_NUM)
  157.                 error("Non-numeric for epsilon");
  158.             setepsilon(vp->v_num);
  159.             break;
  160.  
  161.         case CONFIG_MAXPRINT:
  162.             if (vp->v_type != V_NUM)
  163.                 error("Non-numeric for maxprint");
  164.             q = vp->v_num;
  165.             temp = qtoi(q);
  166.             if (qisfrac(q) || qisneg(q) || !istiny(q->num))
  167.                 temp = -1;
  168.             if (temp < 0)
  169.                 error("Maxprint value is out of range");
  170.             maxprint = temp;
  171.             break;
  172.  
  173.         case CONFIG_MUL2:
  174.             if (vp->v_type != V_NUM)
  175.                 error("Non-numeric for mul2");
  176.             q = vp->v_num;
  177.             temp = qtoi(q);
  178.             if (qisfrac(q) || qisneg(q))
  179.                 temp = -1;
  180.             if (temp == 0)
  181.                 temp = MUL_ALG2;
  182.             if (temp < 2)
  183.                 error("Illegal mul2 value");
  184.             _mul2_ = temp;
  185.             break;
  186.  
  187.         case CONFIG_SQ2:
  188.             if (vp->v_type != V_NUM)
  189.                 error("Non-numeric for sq2");
  190.             q = vp->v_num;
  191.             temp = qtoi(q);
  192.             if (qisfrac(q) || qisneg(q))
  193.                 temp = -1;
  194.             if (temp == 0)
  195.                 temp = SQ_ALG2;
  196.             if (temp < 2)
  197.                 error("Illegal sq2 value");
  198.             _sq2_ = temp;
  199.             break;
  200.  
  201.         case CONFIG_POW2:
  202.             if (vp->v_type != V_NUM)
  203.                 error("Non-numeric for pow2");
  204.             q = vp->v_num;
  205.             temp = qtoi(q);
  206.             if (qisfrac(q) || qisneg(q))
  207.                 temp = -1;
  208.             if (temp == 0)
  209.                 temp = POW_ALG2;
  210.             if (temp < 1)
  211.                 error("Illegal pow2 value");
  212.             _pow2_ = temp;
  213.             break;
  214.  
  215.         case CONFIG_REDC2:
  216.             if (vp->v_type != V_NUM)
  217.                 error("Non-numeric for redc2");
  218.             q = vp->v_num;
  219.             temp = qtoi(q);
  220.             if (qisfrac(q) || qisneg(q))
  221.                 temp = -1;
  222.             if (temp == 0)
  223.                 temp = REDC_ALG2;
  224.             if (temp < 1)
  225.                 error("Illegal redc2 value");
  226.             _redc2_ = temp;
  227.             break;
  228.  
  229.         default:
  230.             error("Setting illegal config parameter");
  231.     }
  232. }
  233.  
  234.  
  235. /*
  236.  * Get the current value of the specified configuration type.
  237.  * An error is generated if the type number is illegal.
  238.  */
  239. void
  240. getconfig(type, vp)
  241.     VALUE *vp;
  242. {
  243.     switch (type) {
  244.         case CONFIG_TRACE:
  245.             vp->v_type = V_NUM;
  246.             vp->v_num = itoq((long) traceflags);
  247.             break;
  248.  
  249.         case CONFIG_DISPLAY:
  250.             vp->v_type = V_NUM;
  251.             vp->v_num = itoq(_outdigits_);
  252.             break;
  253.  
  254.         case CONFIG_MODE:
  255.             vp->v_type = V_STR;
  256.             vp->v_subtype = V_STRLITERAL;
  257.             vp->v_str = modename(_outmode_);
  258.             break;
  259.  
  260.         case CONFIG_EPSILON:
  261.             vp->v_type = V_NUM;
  262.             vp->v_num = qlink(_epsilon_);
  263.             break;
  264.  
  265.         case CONFIG_MAXPRINT:
  266.             vp->v_type = V_NUM;
  267.             vp->v_num = itoq(maxprint);
  268.             break;
  269.  
  270.         case CONFIG_MUL2:
  271.             vp->v_type = V_NUM;
  272.             vp->v_num = itoq(_mul2_);
  273.             break;
  274.  
  275.         case CONFIG_SQ2:
  276.             vp->v_type = V_NUM;
  277.             vp->v_num = itoq(_sq2_);
  278.             break;
  279.  
  280.         case CONFIG_POW2:
  281.             vp->v_type = V_NUM;
  282.             vp->v_num = itoq(_pow2_);
  283.             break;
  284.  
  285.         case CONFIG_REDC2:
  286.             vp->v_type = V_NUM;
  287.             vp->v_num = itoq(_redc2_);
  288.             break;
  289.  
  290.         default:
  291.             error("Getting illegal config parameter");
  292.     }
  293. }
  294.  
  295. /* END CODE */
  296.